<?php if ( ! DEFINED('BASEPATH') ) die('No direct script access allowed.');

/**
 * @author 		Mark Skilbeck
 * @copyright 	Copyright (c) 2009, Mahcuz.com, Inc.
 * @package		PPT
 * @link		http://mahcuz.com/php-project-tracker/wiki/project_model
 */

class Project_model extends Model {
	
	/**
	 * @access 	private
	 * @var		int
	 */
	var $_project_id = NULL;
	/**
	 * @access 	private
	 * @var		string
	 */
	var $_project_name = NULL;
	/**
	 * @access 	private
	 * @var		string
	 */
	var $_project_author = NULL;
	/**
	 * @access 	private
	 * @var		string
	 */
	var $_project_description = NULL;
	/**
	 * @access 	private
	 * @var		string
	 */
	var $_project_alias = NULL;
	/**
	 * @access	private
	 * @var		bool
	 */
	var $_project_has_release = NULL;
	
	/**
	 * PROJ_ALIAS_URI
	 * 
	 * This must refer to the position in URI in which the project 
	 * alias resides - usually 3.
	 * 
	 * @access 	pulic
	 * @var		string
	 */
	const PROJ_ALIAS_URI = 4;
	
	// --------------------------------------------------------------------------------
	
	/**
	 * Project::Project()
	 * 
	 * @return void
	 */
	function Project_model ( )
	{
		# Call our parents constructor to gain access to libraries and such.
		parent::Model();
		
		# We'll only set our poperties if we have an alias.
		if ( $this->uri->segment( self::PROJ_ALIAS_URI ) !== FALSE && 
			 $this->project_exists_by_alias( $this->uri->segment( self::PROJ_ALIAS_URI ) ) !== FALSE ) 
		{
			# Store the project info.
			$project = $this->get_project_by_alias( $this->uri->segment( self::PROJ_ALIAS_URI ) );
			
			# Set each of our properties via the $project variable.
			$this->_project_id 			= (int)		$project->id;
			$this->_project_name 		= (string)	$project->project_name;
			$this->_project_author  	= (string)	$project->project_author;
			$this->_project_description = (string)	$project->project_desc;
			$this->_project_alias 		= (string)	$project->alias;	
			$this->_project_has_release = (bool) 	$project->has_release;	
		}		
	}
	
	/**
	 * Project::__get()
	 * 
	 * All properties of this class are private and can be accessed 
	 * via omitting '_project_' from the property name.
	 * 
	 * @param   mixed $property
	 * @return  mixed
	 */
	function __get( $property )
	{
		# Format the property name.
		$property = '_project_' . $property;
		
		# Check it exists in our class.
		if ( isset ( $this->$property ) )
		{
			# return the property
			return $this->$property;
		}
	}
	
	// ----------------------------------------------------------
	
	/**
	 * Project::get_project()
	 * 
	 * Will return a single project that resolves to the given $proj_name
	 * 
	 * @param 	string $alias	Alias of project to search for
	 * @return	object or NULL
	 */
	function get_project( $proj_name )
	{
		$this->db	
			 ->from('projects')
			 ->where('project_name', $proj_name)
			 ->limit(1);
		$query = $this->db->get();
		
		if ( $query->num_rows() < 1 )
		{
			return NULL;
		}
		return $query->row();
	}
	/**
	 * Project_model::get_project_by_alias()
	 * 
	 * Brother method of Project_model::get_project()
	 * Takes alias instead of project name.
	 * 
	 * @param  string $alias Alias of project to search for
	 * @return obj, or null
	 */
	function get_project_by_alias( $alias )
	{
		$this->db	
			 ->from('projects')
			 ->where('alias', $alias)
			 ->limit(1);
		$query = $this->db->get();
		if ( $query->num_rows() < 1 )
		{
			return NULL;
		}
		else
		{
			return $query->row();
		}
	}
	
	/**
	 * Project_model::get_all_projects()
	 * 
	 * Will return all projects in the ppt_projects table
	 * 
	 * @return obj, or null
	 */
	function get_all_projects()
	{
		$this->db
			 ->from('projects');
	 	$query = $this->db->get();
 		if ( $query->num_rows() < 1 )
 	 	{
 	 		return NULL;
 	 	}
 	 	else
 	 	{
 	 		return $query;
 	 	}
	}
	
	/**
	 * Project_model::create_project()
	 * 
	 * @param  array $data 		An associative array of data to use for update
	 * @return void
	 */
	function create_project( array $data )
	{
		$this->db
			 ->insert('projects', $data);
	}
	
	/**
	 * Project_model::update_project()
	 * 
	 * @param  string $project  The project name to search for
	 * @param  array  $data		An associative array of data to use for update
	 * @return void
	 */
	function update_project( $alias, array $data )
	{
		$this->db
			 ->where('alias', $alias)
 			 ->update('projects', $data);
	}
	
	/**
	 * Project_model::delete_project()
	 * 
	 * @param  string $project The project name to search for
	 * @return void
	 */
	function delete_project( $project )
	{
		$this->db	
			 ->where('project_name', $project)
		     ->delete('projects');
	}
	
	/**
	 * Project::project_exists_by_alias()
	 * 
	 * Checks that a project exists with the given alias.
	 * Similar to project_exists(), but takes and compares aliases
	 * instead of project names.
	 * 
	 * @param	string Alias of project to search for
	 * @access	public
	 * @return  bool
	 */
	function project_exists_by_alias ( $alias ) 
	{
		$this->db
			 ->from('projects')
			 ->where('alias', $alias);
		$query = $this->db->get();
		
		if ( $query->num_rows() < 1 )
		{
			return FALSE;
		}
		else
		{
			return TRUE;
		}
	}
	
	// ----------------------------------------------------------
	
	/**
	 * Project_model::delete_changelogs()
	 * 
	 * @param  string $project The project name to search for
	 * @return void
	 */
	function delete_changelogs ( $project )
	{
		$this->db
			 ->where('project_name', $project)
			 ->delete('project_changelogs');
	}
	
	// ----------------------------------------------------------
	
	/**
	 * Project_model::add_release()
	 * 
	 * Adds release data into database.
	 * 
	 * @param  array $data Associative array of data to insert.
	 * @return void
	 */
	public function add_release ($data)
	{
		$this->db
			 ->insert('project_releases', $data);
	}
	
	/**
	 * Project_model::get_releases()
	 *
	 * @param  int $pid Returns releases for project with this project id.
	 * @return   object, or null
	 */
	public function get_releases($pid)
	{
		$this->db
			   ->from('project_releases')
			   ->where('project_id', $pid);
		$query = $this->db->get();
		
		if ($query->num_rows() > 0)
		{
			return $query;
		}
		else 
		{
			return NULL;
		}
	}
	
	public function get_release($release_version, $pid)	
	{
		$this->db
			   ->from('project_releases')
			   ->where('project_id', $pid)
				   ->where('release_version', $release_version);
		$query = $this->db->get();
		
		if ($query->num_rows() > 0)
		{
			return $query->row();
		}
		else 
		{
			return NULL;
		}
	}
	
	public function update_release($release_id, $data)
	{
		$this->db
			   ->where('id', $release_id)
			   ->update('project_releases', $data);
	}
	
	public function release_exists($release_version, $pid)	
	{
		$this->db
			   ->from('project_releases')
			   ->where('project_id', $pid)
				   ->where('release_version', $release_version);
		$query = $this->db->get();
		
		if ($query->num_rows() > 0)
		{
			return TRUE;
		}
		else 
		{
			return FALSE;
		}
	}
	
	/**
	 * Project_model::get_release_count_by_alias()
	 * 
	 * @param  string $project Project name to get releases for.
	 * @return int
	 */
	public function get_release_count_by_alias ($alias)
	{
		$pid = $this->db->from('projects')->where('alias', $alias);
		$pid = $this->db->get();
		if ($pid->num_rows() > 0)
		{
			$pid = $pid->row();
			$this->db
				 ->from('project_releases')
			 	 ->where('project_id', $pid->id);
			$query = $this->db->get();
			return $query->num_rows();
		}
	} 
	
	/**
	 * Project_model::delete_releases()
	 * 
	 * @param  string $project The project name to search for
	 * @return void
	 */
	function delete_releases ( $project )
	{
		$this->db
			 ->where('project_name', $project)
			 ->delete('project_releases');
		
	}
	
}

?>